package org.ladybug.web.controller.form;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ladybug.service.CommandService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class ManageFormController extends SimpleFormController {

	private CommandService commandService;
	
	

	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.DATE_FIELD, request.getLocale());
		df.applyPattern("dd/MM/yyyy");
		CustomDateEditor ce = new CustomDateEditor(df,true);
		ce.addPropertyChangeListener(new PropertyChangeListener(){

			public void propertyChange(PropertyChangeEvent evt) {
				System.out.println(".propertyChange() "+evt.getPropertyName()+": "+evt.getOldValue()+" --> "+evt.getNewValue());
			}
			
		});
		binder.registerCustomEditor(Date.class, ce);
	}

	/**
	 * valore dell'attributo name dell' <input type="submit"/>
	 */
	private String buttonName;
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		System.out.println("ManageFormController.onSubmit()");
		
		System.out.println("ManageFormController.onSubmit() CommandService: "
				+ commandService);
		String action = ServletRequestUtils.getRequiredStringParameter(request, getButtonName());
		if(action.equalsIgnoreCase("inserisci")){
			commandService.insertOrUpdate(command, errors);
		}
		if(action.equalsIgnoreCase("delete")){
			commandService.delete(command, errors);
		}
		ModelAndView maw = new ModelAndView(getSuccessView(),errors.getModel());
		return maw;
	}

	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		System.out.println("ManageFormController.referenceData()");
		return super.referenceData(request, command, errors);
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		System.out.println("ManageFormController.handleRequestInternal()");
		return super.handleRequestInternal(arg0, arg1);
	}

	public CommandService getCommandService() {
		return commandService;
	}

	public void setCommandService(CommandService commandService) {
		this.commandService = commandService;
	}

	public String getButtonName() {
		return buttonName;
	}

	public void setButtonName(String buttonName) {
		this.buttonName = buttonName;
	}

}
